<template>
	<view>
		<view class="detail" v-if="!enterprise">
			<view class="title">修改备注：</view>
			<view class="change">

				<input class="choose" type="text" @input="changeNotes">
				<view class="button" @click="sendNotes">
					提交
				</view>
			</view>
			<view class="title">修改分组：</view>
			<view class="change">
				<view class="choose">
					<picker :value="friendGroupindex" :range="friendGroupList" @change="friendGroupChange">
						<view class="">{{friendGroupList[friendGroupindex]}}</view>
					</picker>
				</view>
				<view class="button" @click="sendGroupName">
					提交
				</view>
			</view>
			<view class="send" @click="goToFind" style="background-color: #2B9939;">
			    进入好友朋友圈
			</view>
			<view class="send" @click="delFriend">
				删除好友
			</view>
		</view>

		<view class="detail" v-else>
			<view class="title">修改分组：</view>
			<view class="change">
				<view class="choose">
					<picker :value="staffGroupindex" :range="staffGroupList" @change="staffGroupChange">
						<view class="">{{staffGroupList[staffGroupindex]}}</view>
					</picker>
				</view>

				<view class="button" @click="sendStaffGroupName">
					提交
				</view>
			</view>

			<view class="title">修改职务：</view>
			<view class="change">

				<input class="choose" placeholder="请输入职务名称:" type="text" @input="changePosition">
				<view class="button" @click="sendPosition">
					提交
				</view>
			</view>

			<view class="send" v-if="!admin" @click="giveAdmin">
				转让管理员
			</view>

			<view class="send" v-if="!admin" @click="delStaff">
				删除员工
			</view>
		</view>
	</view>
</template>

<script>
	import {
		socket
	} from '../../../utils/socket.js';
	export default {
		data() {
			return {
				notes: "",
				groupName: "",
				userId: "",
				friendGroupindex: 0,
				friendGroupList: [],


				enterprise: false,
				staffGroupindex: 0,
				staffGroupList: [],
				position: "",
				admin: false,
				name: ""
			};
		},
		onLoad(e) {
			this.userId = e.userId;
			this.name = e.name;
			uni.setNavigationBarTitle({
				title: e.name
			})
			if (uni.getStorageSync("enterprise").enterpriseId != 0) {
				this.enterprise = true;
				this.staffGroupList = JSON.parse(uni.getStorageSync("enterprise").groupList);
				if (this.userId == uni.getStorageSync("userId")) {
					this.admin = true
				}
			} else {
				this.enterprise = false;
				this.friendGroupList = JSON.parse(uni.getStorageSync("groupList"));
			}
		},
		methods: {
			staffGroupChange(e) {
				this.staffGroupindex = e.detail.value;
			},
			friendGroupChange(e) {
				this.friendGroupindex = e.detail.value;
			},
			changeNotes(e) {
				this.notes = e.detail.value
			},
			changePosition(e) {
				this.position = e.detail.value
			},
			sendStaffGroupName() {
				uni.request({
					url: getApp().url + 'staff/changeStaff',
					data: {
						userId: this.userId,
						enterpriseId: uni.getStorageSync("enterprise").enterpriseId,
						groupName: this.staffGroupList[this.staffGroupindex]
					},
					header: {
						token: uni.getStorageSync("token")
					},
					method: "POST",
					success: (response) => {
						if (response.data.code == -1) {
							return uni.showToast({
								icon: "error",
								title: response.data.msg
							});
						};
						uni.showToast({
							icon: "success",
							title: "成功"
						});

					},
					fail: (error) => {
						console.log(error);
					}
				})
			},
			sendPosition() {
				uni.request({
					url: getApp().url + 'staff/changeStaff',
					data: {
						userId: this.userId,
						enterpriseId: uni.getStorageSync("enterprise").enterpriseId,
						position: this.position
					},
					header: {
						token: uni.getStorageSync("token")
					},
					method: "POST",
					success: (response) => {
						if (response.data.code == -1) {
							return uni.showToast({
								icon: "error",
								title: response.data.msg
							});
						};
						uni.showToast({
							icon: "success",
							title: "成功"
						});

					},
					fail: (error) => {
						console.log(error);
					}
				})
			},
			sendNotes() {
				uni.request({
					url: getApp().url + 'friendList/updateFriend',
					data: {
						userId: uni.getStorageSync("userId"),
						friendId: this.userId,
						notes: this.notes
					},
					header: {
						token: uni.getStorageSync("token")
					},
					method: "POST",
					success: (response) => {
						if (response.data.code == -1) {
							return uni.showToast({
								icon: "error",
								title: response.data.msg
							});
						};
						uni.showToast({
							icon: "success",
							title: "成功"
						});

					},
					fail: (error) => {
						console.log(error);
					}
				})

				uni.setNavigationBarTitle({
					title: this.notes
				})
			},
			sendGroupName() {
				uni.request({
					url: getApp().url + 'friendList/updateFriend',
					data: {
						userId: uni.getStorageSync("userId"),
						friendId: this.userId,
						groupName: this.friendGroupList[this.friendGroupindex]
					},
					header: {
						token: uni.getStorageSync("token")
					},
					method: "POST",
					success: (response) => {
						if (response.data.code == -1) {
							return uni.showToast({
								icon: "error",
								title: response.data.msg
							});
						};
						uni.showToast({
							icon: "success",
							title: "成功"
						});

					},
					fail: (error) => {
						console.log(error);
					}
				})
			},
			giveAdmin() {
				var userId = this.userId
				uni.showModal({
					title: '提示',
					content: '确认要转让管理员给这位员工吗？',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: getApp().url + 'enterprise/changeEnterprise',
								data: {
									adminId: userId,
									enterpriseId: uni.getStorageSync("enterprise").enterpriseId
								},
								header: {
									token: uni.getStorageSync("token")
								},
								method: "POST",
								success: (response) => {
									if (response.data.code == -1) {
										return uni.showToast({
											icon: "error",
											title: response.data.msg
										});
									};

									var enterprise = uni.getStorageSync("enterprise");
									enterprise.adminId = userId;
									uni.setStorageSync("enterprise", enterprise);

									uni.showToast({
										icon: "success",
										title: "成功"
									});

									uni.switchTab({
										url: "/pages/enterprise/index/index"
									})

								},
								fail: (error) => {
									console.log(error);
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			delStaff() {
				var userId = this.userId
				uni.showModal({
					title: '提示',
					content: '确认要删除这位员工吗？',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: getApp().url + 'staff/delStaff',
								data: {
									userId: userId,
									enterpriseId: uni.getStorageSync("enterprise").enterpriseId
								},
								header: {
									token: uni.getStorageSync("token")
								},
								method: "POST",
								success: (response) => {
									if (response.data.code == -1) {
										return uni.showToast({
											icon: "error",
											title: response.data.msg
										});
									};

									uni.showToast({
										icon: "success",
										title: "成功"
									});

									uni.switchTab({
										url: "/pages/enterprise/index/index"
									})

								},
								fail: (error) => {
									console.log(error);
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			delFriend() {
				var userId = this.userId;
				var name = this.name;
				uni.showModal({
					title: '提示',
					content: '确认要删除这位好友吗？',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: getApp().url + 'friendList/delFriend',
								data: {
									userId: uni.getStorageSync("userId"),
									friendId: userId
								},
								header: {
									token: uni.getStorageSync("token")
								},
								method: "POST",
								success: (response) => {
									if (response.data.code == -1) {
										return uni.showToast({
											icon: "error",
											title: response.data.msg
										});
									};
									var message = {
										fromUserId: uni.getStorageSync("userId"),
										toUserId: userId,
										type: 5,
										content: uni.getStorageSync("name"),
										createTime: Date.now(),
										enterpriseId: 0
									}
									//通过已经在登录页建立好的为websocket连接向后端发送消息

									socket.send(JSON.stringify(message))

									uni.showToast({
										icon: "success",
										title: "成功"
									});

									uni.switchTab({
										url: "/pages/person/index/index"
									})

								},
								fail: (error) => {
									console.log(error);
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			goToFind(){
				uni.navigateTo({
					url: "/pages/person/userFind/userFind?userId=" + this.userId
				})
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		margin: 0 5vw;

		.title {
			margin-top: 20px;
			color: #A09D9D;
			line-height: 40px;
		}

		.change {
			display: flex;
			height: 40px;

			.choose {
				width: 70vw;
				height: 40px;
				text-align: center;
				line-height: 40px;
				margin-right: 5vw;
				box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
			}

			.button {
				width: 20vw;
				height: 40px;
				text-align: center;
				line-height: 40px;
				border-radius: 10px;
				background-color: #2B9939;
			}


		}


		.send {
			margin-top: 30px;
			height: 40px;
			line-height: 40px;
			border-radius: 20px;
			text-align: center;
			color: #FFF;
			background-color: red;
		}
	}
</style>